package com.tjsinfo.tjsoa.workTask.action;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONArray;
import org.json.JSONObject;

import com.tjsinfo.tjsoa.workTask.bo.ITDailyWorkTaskBO;
import com.tjsinfo.tjsoa.workTask.vo.TDailyWorkTask;

public class WorkTaskListAction extends Action {
	private ITDailyWorkTaskBO workTaskBo;

	public void setWorkTaskBo(ITDailyWorkTaskBO workTaskBo) {
		this.workTaskBo = workTaskBo;
	}
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		List<TDailyWorkTask> list = null;
		Integer start = 0;
		JSONArray array = new JSONArray();
		JSONObject object = new JSONObject();
		String name = null;
		String personTypeName = null;
		String dividerUserid = request.getParameter("dividerUserid");
		
		if("recipientUserid".equals(dividerUserid)){
			String dividerUsername = request.getParameter("dividerUsername");
			name = dividerUsername;
			personTypeName = "dividerUsername";
		}else{
			String recipientUsername = request.getParameter("recipientUsername");
			name = recipientUsername;
			personTypeName = "recipientUsername";
		}
		
		if (name == null || name == "") {
			if(request.getParameter("start") == null)
			{
				list = workTaskBo.selectAll(0, 10,dividerUserid,request);
			}
			else
			{
				start = Integer.parseInt(request.getParameter("start"));
				list = workTaskBo.selectAll(start, 10,dividerUserid,request);
			}
		} else {
			if(request.getParameter("start") == null)
			{
				list = workTaskBo.selectName(0, 10, dividerUserid, personTypeName, name, request);
			}
			else
			{
				start = Integer.parseInt(request.getParameter("start"));
				list = workTaskBo.selectName(start, 10, dividerUserid, personTypeName, name, request);
			}
		}
		object.put("totalProperty",workTaskBo.selectTotal(dividerUserid, personTypeName, name, request));
		Collections.reverse(list);//倒序排列
		for (TDailyWorkTask dailyWorkTask : list) {
			dailyWorkTask.setBeginTime1(sdf.format(dailyWorkTask.getBeginTime()));
			dailyWorkTask.setEndTime1(sdf.format(dailyWorkTask.getEndTime()));
			switch (dailyWorkTask.getImportLevel()) {
			case 0:
				dailyWorkTask.setImportLevel1("低");
				break;
			case 1:
				dailyWorkTask.setImportLevel1("中");
				break;
			case 2:
				dailyWorkTask.setImportLevel1("高");
				break;
			}
			
			switch (dailyWorkTask.getCurrentStatus()) {
			case 0:
				dailyWorkTask.setCurrentStatus1("未查看");
				break;
			case 1:
				dailyWorkTask.setCurrentStatus1("查看");
				break;
			case 2:
				dailyWorkTask.setCurrentStatus1("办结");
				break;
			case 3:
				dailyWorkTask.setCurrentStatus1("拒绝");
				break;
			case 4:
				dailyWorkTask.setCurrentStatus1("重办");
				break;
			case 5:
				dailyWorkTask.setCurrentStatus1("完结");
				break;
			}
			
			JSONObject workTaskJson = new JSONObject(dailyWorkTask);
			array.put(workTaskJson);
		}
		object.put("root", array);
		response.getWriter().print(object);
		return null;
	}
}
